﻿using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Silverlight.Navigation.History
{
    /// <summary>
    /// Represents a history manager object that uses isolated storage for maintaining
    /// an application's history.
    /// </summary>
    public class IsolatedStorageHistoryManager : HistoryManager
    {
        #region Constructors

        /// <summary>
        /// Initilaizes a new instance of the IsolatedStorageHistoryManager class.
        /// </summary>
        /// <param name="key">The key used by isolated storage for saving and retrieval.</param>
        public IsolatedStorageHistoryManager(string key)
        {
            if (string.IsNullOrEmpty(key))
                throw new ArgumentNullException("key");

            Key = key;
        }

        #endregion

        #region Properties

        /// <summary>
        /// The key used by isolated storage for saving and retrieval.
        /// </summary>
        public string Key { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// Loads the items in the application's history.
        /// </summary>
        protected override void OnLoad()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(Key))
                this.HistoryItems = (HistoryItemCollection)IsolatedStorageSettings.ApplicationSettings[Key];
            else
                this.HistoryItems = new HistoryItemCollection();
        }

        /// <summary>
        /// Saves the items in the application's history.
        /// </summary>
        public override void Save()
        {
            IsolatedStorageSettings.ApplicationSettings[Key] = this.HistoryItems;
        }

        #endregion
    }
}
